package com.campol.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.campol.R;
import com.campol.activity.PostsListingActivity;
import com.campol.model.PostModel;

public class PostsAdapter extends ArrayAdapter<PostModel> {

	private LayoutInflater inflater;
	
	public PostsAdapter(Context context, List<PostModel> items) {
		super(context, 0, 0, items);
		inflater = LayoutInflater.from(context);
	}
	
	@Override
	public int getItemViewType(int position) {
		return PostsListingActivity.ITEM_TYPE_POST;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		RelativeLayout rl = (RelativeLayout)convertView;
		
		ViewHolder viewHolder;
		if (rl == null) {
			viewHolder = new ViewHolder();
			rl = (RelativeLayout) inflater.inflate(R.layout.post_item_layout, null, false);
			viewHolder.title = (TextView) rl.findViewById(R.id.post_item_title);
			viewHolder.summary = (TextView) rl.findViewById(R.id.post_item_summary);
			viewHolder.distance = (TextView) rl.findViewById(R.id.post_item_distance);
			
			rl.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) rl.getTag();
		}

		PostModel item = getItem(position);
		
		viewHolder.title.setText(item.Road);
		if (item.Place!=null) {
			String place = item.Place.length() > 30 ? item.Place.substring(0,30)+"...":item.Place;
			viewHolder.summary.setText(place); 
		}
		viewHolder.distance.setText(item.Distance); 
		
		return rl;
	}

	private class ViewHolder {
		public TextView title;
		public TextView summary;
		public TextView distance;
	}
}
